package westh.ilib.struts.action.manage.researcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.config.Config;
import westh.ilib.struts.action.manage.resource.ResourceAddAction;
import westh.util.YyAction;
import westh.util.YyID;

public class ResearcherAddAction  extends YyAction {
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
	.getLog(ResourceAddAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");

	public ActionForward doAction(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response) {
	// 日志和权限检查，请勿删除
		westh.log.YyLog.addFootprint(request, "动作－添加研究者");
		
		//DynaActionForm f = (DynaActionForm) form;
		String researcherName = request.getParameter("researcherName");
		String researcherInstitute = request.getParameter("researcherInstitute");
		String researcherHomepage = request.getParameter("researcherHomepage");
		log.debug("in addAction researcherHomepage =" + researcherHomepage);
		
/*		String researcherFamilyName = f.getString("researcherFamilyName");
		String researcherGivenName = f.getString("researcherGivenName");
		String researcherInstitute = f.getString("researcherInstitute");
		String researcherHomepage = f.getString("researcherHomepage");
		*/
		//获得编号
		String strId = YyID.getUUID32();
		/*************************cdoi info start******************************/
		String rId = "012f717f8dde4129a95837bf153de5a0"+strId;
		
		/*
		//形成json进行封装 
		JSONObject jsonCdoi = new JSONObject(); 
		jsonCdoi.put("WESTDO_HANDLE",rId);                                    //唯一标识
		jsonCdoi.put("CDOI_AGENCY_ID", "012f717f8dde4129a95837bf153de5a0");   //注册机构编码
		jsonCdoi.put("DC_LANGUAGE", "c7d2ec860cee4b03ba845da0233ed977");      //语种标识符
		jsonCdoi.put("CDOI_CUSTOMER_ID", "f41825a60cf44e83b83cbd9114220f29"); //用户标识符
		jsonCdoi.put("DC_TYPE", "27278cfe6e1c429aa2197b3ff34a11ab");          //类型标识符
		jsonCdoi.put("DC_TITLE", researcherFamilyName+" "+researcherGivenName);           //题名
		jsonCdoi.put("DC_CREATOR", "");                                		  //创建者
		jsonCdoi.put("DC_SUBJECT", "");                                       //主题
		jsonCdoi.put("DC_DESCRIPTION", "");                           		  //描述
		jsonCdoi.put("DC_PUBLISHER", "");                                     //出版者
		jsonCdoi.put("DC_CONTRIBUTOR", "");                                   //其他责任者
		jsonCdoi.put("DC_DATE", "");                                  		  //日期
		jsonCdoi.put("DC_FORMAT", "");                                        //格式
		jsonCdoi.put("DC_IDENTIFIER", "");                                    //标识符
		jsonCdoi.put("DC_SOURCE", "");                                        //来源
		jsonCdoi.put("DC_RELATION", "");                                      //关联
		jsonCdoi.put("DC_COVERAGE", "");                                      //覆盖范围                         
		jsonCdoi.put("DC_RIGHTS", "");                                        //权限
		jsonCdoi.put("CDOI_URL_SOURCE", researcherHomepage);                        //资源URL
		jsonCdoi.put("CDOI_STATUS", "1");                                     //资源注册状态，2审核通过
		jsonCdoi.put("CDOI_NOTE", "");                                        //备注
		
		String cdoiJson = jsonCdoi.toString();
		String cdoiId="";
		try{
		  MuleClient clientW = new MuleClient();
		//  UMOMessage umo = clientW.send("axis:http://192.168.28.112:4281/Cdoi/cdoiCdoi?method=addJson",cdoiJson,null);
		  UMOMessage umo = clientW.send("axis:http://localhost:4281/Cdoi/cdoiCdoi?method=addJson",cdoiJson,null);
		  cdoiId = (String)umo.getPayload();
		} catch (Exception e) {
		throw new RuntimeException(e);
		}
		*/
		/** ****************************cdoi info end************************ *//*

		/** ****************************cdo info start************************ */
		JSONObject jsonObj = new JSONObject();
		
		jsonObj.put("researcherId", rId);                        // researcher Id
		jsonObj.put("researcherName", researcherName);              // given name
		jsonObj.put("researcherInstitute", researcherInstitute);              // institute
		jsonObj.put("researcherHomepage", researcherHomepage);                // homepage
		String input = jsonObj.toString();

		
		
		String id = "";
		try {
			MuleClient clientD = new MuleClient();
			UMOMessage umo = clientD.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=addResearcherJson",input, null);
			id = (String) umo.getPayload();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		/** ****************************cdo info end************************ */
		if (id.equals("1")) {
			String insertSubjectIndustry = "资源信息添加成功<br/>";
			request.setAttribute("info", insertSubjectIndustry);
			request.setAttribute("redirect", "../researcherAddPre.do");
			return mapping.findForward("info");
		} else {
			String insertSubjectIndustry = "资源信息信息添加失败<br/>";
			request.setAttribute("info", insertSubjectIndustry);
			request.setAttribute("redirect", "../researcherAddPre.do");
			return mapping.findForward("info");
		}
	}

}
